import { _decorator, Color, Component, KeyCode, MeshRenderer, Node } from 'cc';
import { TankMovement } from '../Tank/TankMovement';
import { TankShooting } from '../Tank/TankShooting';
import { TankRenderers } from '../Tank/TankRenderers';
import { TankHealth } from '../Tank/TankHealth';
import { TankType } from '../Enum/TankType';
import { Grid } from '../Astar/Grid/Grid';
import { TankAstar } from '../Tank/TankAstar';
import { Astarmanager } from '../Astar/Astarmanager';
import { TankAI } from '../Tank/TankAI';


export class TankManager {


    public playerColor: Color;

    public spawnPoint: Node;

    public playNumber: number = 1;

    public grid: Grid = null;

    public instance: Node = null;

    private movement: TankMovement = null;

    private shooting: TankShooting = null;

    private render: TankRenderers = null;

    public health: TankHealth = null;

    public astar: TankAstar = null;

    public tankAI: TankAI = null;

    public tankType: TankType = TankType.Player;

    private fowardKey: KeyCode;
    private backwardKey: KeyCode;
    private leftKey: KeyCode;
    private rightKey: KeyCode;
    private shootingKey: KeyCode;

    constructor(playerColor: Color, spawnPoint: Node, type: TankType) {
        this.playerColor = playerColor;
        this.spawnPoint = spawnPoint;
        this.tankType = type;
    }

    public bindKey(fKey: KeyCode, bKey: KeyCode, lKey: KeyCode, rKey: KeyCode, sKey: KeyCode) {
        this.fowardKey = fKey;
        this.backwardKey = bKey;
        this.leftKey = lKey;
        this.rightKey = rKey;
        this.shootingKey = sKey;
    }

    public setup() {

        this.movement = this.instance.getComponent(TankMovement);
        this.render = this.instance.getComponentInChildren(TankRenderers);
        this.shooting = this.instance.getComponent(TankShooting);
        this.health = this.instance.getComponent(TankHealth);

        this.movement.tankType = this.tankType;
        this.shooting.tankType = this.tankType;
        if (this.tankType == TankType.Player) {
            this.movement.bindKey(this.fowardKey, this.backwardKey, this.leftKey, this.rightKey);
            this.shooting.bindKey(this.shootingKey);
            Astarmanager.getInstance().tank = this;
        }
        if (this.tankType == TankType.enemy) {
            this.astar = this.instance.getComponent(TankAstar);
            this.astar.Init(this.grid, Astarmanager.getInstance().tank.instance);
            this.astar.enabled = true;
            this.tankAI = this.instance.getComponent(TankAI);
            this.tankAI.enabled = true;
        }


        this.movement.playerNumber = this.playNumber;
        this.shooting.playerNumber = this.playNumber;
        this.render.playerColor = this.playerColor;
        this.render.renderColor();
    }

    public disableControl() {
        this.movement.enabled = false;
        this.shooting.enabled = false;
    }

    public enableControl() {
        this.movement.enabled = true;
        this.shooting.enabled = true;
    }

    public Reset() {
        this.instance.position = this.spawnPoint.position;
        this.instance.rotation = this.spawnPoint.rotation;
    }
}



